package com.zmj.controller;

import com.alibaba.fastjson.JSON;
import com.zmj.entity.User;
import com.zmj.service.IUserService;
import com.zmj.service.impl.UserServiceImpl;
import com.zmj.utils.Message;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    IUserService userService=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String userName=req.getParameter("userName");
       String password=req.getParameter("password");
        System.out.println(userName);
        User user=new User();
        user.setUserName(userName);
        user.setPassword(password);
       User user1= userService.selUserByNameAndPass(user);
        Message message=new Message();
       if (user1==null){
           message.setCode(0);
           message.setMes("user is not exist ");
       }else {
           message.setCode(1);
       }
        resp.getWriter().print(JSON.toJSONString(message));
    }
}
